package com.youknowme.core;

import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.youknowme.utility.LocalyticsHelper;

public abstract class ActivityWrapper extends Activity implements Notifyable {

	private static final String TAG = "GTGJ_ActivityWrapper";
	private Handler mTagBackgroundHandler = null;
	private Runnable mTagBackgroundRunnable = null;
	private ApplicationWrapper mApplication = null;
	private Handler mNotifyHandler = null;
	public static Activity onCreateActivity;
	public static Activity onResumeActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		onCreateActivity = this;
		ApplicationWrapper.IS_FOREGROUND = true;
		mApplication = (ApplicationWrapper) getApplication();
	}

	/*
	 * 获取Application的Context
	 */
	public Context getContext() {
		return getApplication();
	}

	/*
	 * 获取Activity的Context
	 */
	public Context getSelfContext() {
		return this;
	}
	
	/*
	 * 发送广播通知
	 */
	public void sendNotificationBroad(int what, Bundle data) {
		ApplicationWrapper.notifyHandlerPool(what, data);
	}
	
	/*
	 * 发送广播通知
	 */
	public void sendNotificationBroad(int what) {
		sendNotificationBroad(what, null);
	}
	
	/*
	 * 发现路由通知
	 */
	public void sendRouteNotificationRoute(String[] activityNames, int what, Bundle data) {
		ApplicationWrapper.notifyHandlers(activityNames, what, data);
	}
	
	@Override
	public OnPageNotifyListener generatePageNotifyListener() {
		return null;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		LocalyticsHelper.tagScreenStart(this, this.getClass().getName());
		LocalyticsHelper.startSession(this);
		onResumeActivity = this;
		/*
		 * 注册通知
		 */
		
		if (mNotifyHandler == null) {
			final OnPageNotifyListener notifyListener = generatePageNotifyListener();
			mNotifyHandler = new Handler(new Handler.Callback() {
				@Override
				public boolean handleMessage(Message msg) {
					switch (msg.what) {
					}
					
					if (null != notifyListener) {
						notifyListener.onNotify(msg);
					}
					return false;
				}
			});
			
			ApplicationWrapper.pushHandlerPool(this.getClass().getName(), mNotifyHandler);
		}
		
		if (ApplicationWrapper.IS_FOREGROUND == false) {
			
			/*
			 * 如果唤醒时候，发现后台时间超过10分钟，发通知
			 */
//			if (mApplication.getBackgroundTime() != 0) {
//				
//				long gapMin = (System.currentTimeMillis() - mApplication.getBackgroundTime()) / 1000 / 60;
//				if (gapMin >= 10) {
//					
//					sendNotificationBroad(Constants.Notification.WHAT_WAKE_UP_WHEN_BACKGROUND_LONG_TIME);
//					
//					doTimeoutWhenBackground();
//				}
//			}
//			
//			AnalyticsAgent.startSession(getSelfContext());
//			AnalyticsAgent.tagAndroidOpen(getSelfContext());
			
			ApplicationWrapper.IS_FOREGROUND = true;
			
			Log.d(TAG, "FOREGROUND");
		}
		
//		try {
//			String shortClassName = this.getClass().getSimpleName();
//			AnalyticsAgent.tagScreen("android." + shortClassName);
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
	}
	

	@Override
	protected void onPause() {
		onResumeActivity = null;
		if (mTagBackgroundHandler == null || mTagBackgroundRunnable == null) {
			/*
			 * 设置程序后台标识
			 */
			mTagBackgroundHandler = new Handler();
			mTagBackgroundRunnable = new Runnable() {
				@Override
				public void run() {
					final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
					List<RunningAppProcessInfo> Applist = activityManager.getRunningAppProcesses();
					Iterator<RunningAppProcessInfo> l = Applist.iterator();
					String packegName = getPackageName();
					while (l.hasNext()) {
						RunningAppProcessInfo AppInfo = (RunningAppProcessInfo) l.next();
						if (AppInfo.processName.equals(packegName)) {
							if (AppInfo.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
								//AnalyticsAgent.endSession();
								ApplicationWrapper.IS_FOREGROUND = false;// 保存应用程序当前的状态
								Log.d(TAG, "BACKGROUND");
							}
							break;
						}
					}
				}
			};
		}
		mTagBackgroundHandler.removeCallbacks(mTagBackgroundRunnable);
		mTagBackgroundHandler.postDelayed(mTagBackgroundRunnable, 1000);
		
		super.onPause();
		LocalyticsHelper.tagScreenEnd(this, this.getClass().getName());
		LocalyticsHelper.endSession(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		ApplicationWrapper.popHandlerPool(this.getClass().getName());
		mNotifyHandler = null;
	}
}
